package com.easyfile.easyfilemaster.utils;


import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 发送短信工具类
 * @author chenpeman
 */
public class SmsUtils {

    /**
     * 短信提供商url.
     */
    public static String  smsApiUrl = "https://sms_developer.zhenzikj.com";
    /**
     * 短信appid.
     */
    public static String smsAppId = "";
    /**
     * 短信密钥.
     */
    public static String smsAppSecret ="";
    /**
     * 短信模板.
     */
    public static String smsTemplateId = "";

    /**
     * 获取短信
     *
     * @param mobile 手机号
     * @return 验证码
     */
    public static String getSms(String mobile){
        //短信模板
        String templateId = smsTemplateId;
        //生成验证码
        String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String[] templateParams = {verifyCode, "5"};
        Map<String, Object> params = new HashMap<>();
        params.put("number", mobile);
        params.put("templateId", templateId);
        params.put("templateParams", templateParams);

        ZhenziSmsClient client = new ZhenziSmsClient(smsApiUrl,smsAppId,smsAppSecret);
        String result = null;
        try {
            result = client.send(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject json = JSONObject.parseObject(result);
        if (json.getIntValue("code") != 0) {
            //发送短信失败
            return null;
        } else {
            return verifyCode;
        }
    }
}
